package com.qdsg.ysg.doctor.ui;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;

import androidx.annotation.NonNull;
import butterknife.BindView;
import me.panpf.sketch.SketchImageView;
import me.panpf.sketch.decode.ImageAttrs;
import me.panpf.sketch.request.CancelCause;
import me.panpf.sketch.request.DisplayListener;
import me.panpf.sketch.request.ErrorCause;
import me.panpf.sketch.request.ImageFrom;
import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseActivity;
import com.qdsg.ysg.doctor.util.ToastUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2018/6/8.
 */

public class BigImageActivity extends BaseActivity {
    @BindView(R.id.scaleview)
    SketchImageView scaleview;

    String url;
    int type;

    public ProgressDialog dialog;

    @Override
    protected void initViews() {
        url = getIntent().getStringExtra("url");
        type = getIntent().getIntExtra("type", 0);
        scaleview.setZoomEnabled(true);
        scaleview.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                scaleview.setDrawingCacheEnabled(true);
                Bitmap bitmap = Bitmap.createBitmap(scaleview.getDrawingCache());
                scaleview.setDrawingCacheEnabled(false);
                saveBitmap(bitmap);
                return false;
            }
        });
        scaleview.setDisplayListener(new DisplayListener() {
            @Override
            public void onStarted() {

            }

            @Override
            public void onCompleted(@NonNull Drawable drawable, @NonNull ImageFrom imageFrom, @NonNull ImageAttrs imageAttrs) {
                dialog.dismiss();
            }

            @Override
            public void onError(@NonNull ErrorCause cause) {

            }

            @Override
            public void onCanceled(@NonNull CancelCause cause) {

            }
        });
        if (type == 1) {
            scaleview.displayImage(url);
        } else if (type == 2) {
            scaleview.displayImage(url);
        }

    }

    @Override
    protected void initData() {
        dialog = ProgressDialog.show( BigImageActivity.this, "提示", "加载中…", true, false, null);
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.aty_big_image;
    }


    public String saveBitmap(Bitmap bitmap) {
        try {
            // 获取内置SD卡路径
            String sdCardPath = Environment.getExternalStorageDirectory().getPath();
            String imgNameOri = "HomePic_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

            // 图片文件路径
//            File file = new File(sdCardPath);
//            File[] files = file.listFiles();
//            for (int i = 0; i < files.length; i++) {
//                File file1 = files[i];
//                String name = file1.getName();
//            }
            String filePath = sdCardPath + "/" + imgNameOri+".png";
            File file = new File(filePath);
            FileOutputStream os = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
            os.flush();
            os.close();

            //把文件插入到系统图库
            MediaStore.Images.Media.insertImage(getContentResolver(),
                    file.getAbsolutePath(), imgNameOri+".png", null);

            //保存图片后发送广播通知更新数据库
            Uri uri = Uri.fromFile(file);
            sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
            ToastUtil.showToast( mContext, "保存成功" );
            return filePath;
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }
}
